from twisted.web.client import Agent
from twisted.internet.protocol import Protocol
from twisted.internet.defer import Deferred


class ResponseProducer(Protocol):
    def __init__(self, deferred):
        self.deferred = deferred
        self.body = ''

    def dataReceived(self, bytes):
        self.body += bytes

    def connectionLost(self, reason):
        self.deferred.callback(self.body)


class SiftAgent(Agent):

    persistent=True
    maxConnections = 2

    def cbRequest(self, response):
        print 'Response code:', response.code
        finished = Deferred()
        response.deliverBody(ResponseProducer(finished))
        return finished
